package com.losl.framework.web.service;

import com.losl.common.core.domain.model.LoginUser;
import com.losl.common.util.ServletUtils;
import com.losl.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * 权限处理服务类
 *
 * @author Maxwell
 */
@Service("ps")
public class PermissionService {
    @Autowired
    private TokenService tokenService;

    public Boolean hasPermission(String permission) {
        LoginUser loginUser = null;
        if (StringUtils.isEmpty(permission)) {
            return false;
        }
        loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        System.out.println("login"+loginUser);
        if (StringUtils.isNull(loginUser)) {
            return false;
        }
        Set<String> permissions = loginUser.getPermissions();
        return hasPermission(permissions, permission);
    }

    /**
     * 查看用户是否具有权限
     */
    public Boolean hasPermission(Set<String> permissions, String permission) {
        return permissions.contains("*") | permissions.contains(permission);
    }

}
